package com.demo.easyexcel.cotroller;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@RestController
@RequestMapping("/user")  // localhost:8888/user/export
public class UserCotroller {

    @GetMapping("/export")
    public void exportData(HttpServletResponse response)throws Exception{


        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");

        OutputStream outputStream = response.getOutputStream() ;

        //---------------------------------------------------------------------------------------

        XSSFWorkbook workbook = new XSSFWorkbook() ;

        Sheet sheet00 = workbook.createSheet("目录") ;

        Row row = sheet00.createRow(0) ;
        Cell cell = row.createCell(0) ;
        cell.setCellFormula("hyperlink(\"#模板_01!A1\",\"模板_01\")");
        row = sheet00.createRow(1) ;
        cell = row.createCell(0) ;
        cell.setCellFormula("hyperlink(\"#模板_02!A1\",\"模板_02\")");
        row = sheet00.createRow(2) ;
        cell = row.createCell(0) ;
        cell.setCellFormula("hyperlink(\"#模板_03!A1\",\"模板_03\")");
        row = sheet00.createRow(3) ;
        cell = row.createCell(0) ;
        cell.setCellFormula("hyperlink(\"#模板_04!A1\",\"模板_04\")");
        row = sheet00.createRow(4) ;
        cell = row.createCell(0) ;
        cell.setCellFormula("hyperlink(\"#模板_05!A1\",\"模板_05\")");

        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }



}
